<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/2/19
 * Time: 10:39
 */
namespace app\modules\setting\models\seller;

use app\models\QlTiktokShop;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

class QlTiktokShopSearch extends QlTiktokShop
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['data'], 'string'],
            [['shop_code'], 'required'],
            [['seller_id', 'shop_code'], 'string', 'max' => 50],
            [['shop_name', 'shop_icon', 'phone', 'phone_region_code', 'whatsapp', 'whatsapp_region_code', 'email'], 'string', 'max' => 300],
            [['shop_code'], 'unique'],
        ];
    }
    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @param integer $pageSize
     *
     * @return ActiveDataProvider
     */
    public function search($params,$select=null)
    {
        $query = QlTiktokShop::find()->select($select ? $select : "*")->asArray();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => ['pageSize' => ArrayHelper::getValue($params,"pageSize",10),'defaultPageSize' => 10,'pageSizeParam' => 'pageSize'],
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]],
        ]);

        $this->load($params,"");

        if (!$this->validate()) {
            // 如果您不想在验证失败时返回任何记录，请取消下面一行的注释
             $query->where('0=1');
            return $dataProvider;
        }

        // 过滤条件
        $query->andFilterWhere([
            'id' => $this->id,
        ]);


        $query->andFilterWhere([
            'or',
            ['like', 'shop_code', $this->shop_code],
            ['like', 'shop_name', $this->shop_code]
        ]);
//        echo $query->createCommand()->rawSql;exit();

        return $dataProvider;
    }
}